﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace chungcu
{
    public partial class phongdathue : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("phongthue");
        private SqlDataAdapter da = new SqlDataAdapter();

        private void connect()
        {

            String cn = "Data source=(local);Initial Catalog=Quanlychungcu;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open(); //mo ket noi

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public phongdathue()
        {
            InitializeComponent();
        }

        private void phongdathue_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            binding();
        }

        private void getdata()
        {
            SqlCommand command = new SqlCommand(); // khai bao 1 command
            command.Connection = conn; // ket noi
            command.CommandType = CommandType.Text; // khai bao kieu command
            command.CommandText = @"select  ptid,ptcmnd,ktten from phongthue,khachthue where ptcmnd = ktcmnd"; // viet cau lenh SQL
            da.SelectCommand = command; //gan coomand cho da
            da.Fill(dt);//nap du lieu cho table
            showphongthue.DataSource = dt; //load du lieu len datagridview
        }

        private void binding()
        {

            txtsophog.DataBindings.Clear();
            txtsophog.DataBindings.Add("Text", showphongthue.DataSource, "ptid");
            txtcmnd.DataBindings.Clear();
            txtcmnd.DataBindings.Add("Text", showphongthue.DataSource, "ptcmnd");
            txttenkhach.DataBindings.Clear();
            txttenkhach.DataBindings.Add("Text", showphongthue.DataSource, "ktten");

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            disconnect();
        }

       
    }
}
